//
//  ruleViewController.m
//  gpos_app
//
//  Created by apple on 2018/10/16.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ruleViewController.h"
#import "Masonry.h"
#import "GPosAPI.h"
#import "SVProgressHUD.h"
#import "XMRegisterViewController.h"

@interface ruleViewController ()
@property (nonatomic,strong) UIView *backgroundViews;
@property (nonatomic,strong) UILabel *titleLab;
@property (nonatomic,strong) NSString *str1;
@property (nonatomic,strong) NSString *str2;
@property (nonatomic,strong) UILabel *descLabel;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIView *viewOne;
@property (nonatomic,strong) UIView *viewTwo;

@end

@implementation ruleViewController

//改变状态栏
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.title = @"财富规则";
    self.navigationController.navigationBar.topItem.title = @"";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.tintColor=[UIColor blackColor];
    
    int userId=[[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] intValue];
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@(userId),@"userId",@(4),@"type", nil];
    __weak typeof(self) weakSelf = self;
    [GPosAPI requestServiceAndSecurtWithParams:dic andCallback:^(id obj) {
        NSLog(@"%@",obj);
        if (obj ==nil)
        {
            [SVProgressHUD showErrorWithStatus:@"请求失败,请检查您的网络状态"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
            
        }else{
            
            if ([[obj objectForKey:@"code"] integerValue] ==200) {
                NSDictionary *dataDic=[obj objectForKey:@"data"];
                weakSelf.str1=[dataDic objectForKey:@"title"];
                weakSelf.str2=[dataDic objectForKey:@"content"];
                [weakSelf initUI];
            }else if ([[obj objectForKey:@"code"] integerValue]==401 || [[obj objectForKey:@"code"] intValue] == 402){
                
                UIStoryboard * mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                XMRegisterViewController *VC = [mainStory instantiateInitialViewController];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                VC.hidesBottomBarWhenPushed=YES;
                //设置当前窗口的根控制器
                [UIApplication sharedApplication].keyWindow.rootViewController=VC;
                [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }
            else{
                
                [SVProgressHUD showErrorWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
            }
            
        }
        
    }];
    
}


-(void)initUI{
    UILabel *lab=[[UILabel alloc] init];
    lab.text=self.str2;
    lab.numberOfLines=0;
    lab.font=[UIFont boldSystemFontOfSize:14];
    NSMutableParagraphStyle  *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    // 行间距设置
    [paragraphStyle  setLineSpacing:8];
    NSMutableAttributedString  *setString = [[NSMutableAttributedString alloc] initWithString:lab.text];
    [setString  addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [lab.text length])];
    
    [lab sizeToFit];
    CGFloat height = [self.str2 boundingRectWithSize:CGSizeMake(self.view.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:14]} context:nil].size.height;
    CGFloat HH ;
    if (IS_IPHONESE || IS_IPHONE5)
    {
        HH=height*1.65;
    }else{
        HH=height*1.6;
        
    }
    self.scrollView=[[UIScrollView alloc]init];
    self.scrollView.frame=CGRectMake(0, kNavbarHeight, self.view.frame.size.width, KmainScreenHeiht - kNavbarHeight - kBottomSafeHeight);
    
    self.scrollView.alwaysBounceVertical=YES;
    self.scrollView.contentSize=CGSizeMake(self.view.frame.size.width, HH+kNavbarHeight + kBottomSafeHeight);
    [self.view addSubview:self.scrollView];
    
    
    self.scrollView.scrollEnabled=YES;
    
    self.scrollView.showsVerticalScrollIndicator=NO;
    self.backgroundViews=[[UIView alloc] init];
    self.backgroundViews.backgroundColor=[UIColor whiteColor];
    self.backgroundViews.frame=CGRectMake(0, 0, self.scrollView.frame.size.width, HH+kNavbarHeight + kBottomSafeHeight);
    [self.scrollView addSubview:self.backgroundViews];
    
    self.viewOne=[[UIView alloc] init];
    self.viewOne.backgroundColor=[UIColor whiteColor];
    self.viewOne.frame=CGRectMake(0,20, self.backgroundViews.frame.size.width, 25);
    [self.backgroundViews addSubview:self.viewOne];
    
    
    self.titleLab=[[UILabel alloc] init];
    self.titleLab.text=[NSString stringWithFormat:@"%@",self.str1];
    self.titleLab.textAlignment=NSTextAlignmentCenter;
    self.titleLab.font=[UIFont systemFontOfSize:17];
    self.titleLab.frame=CGRectMake(0, 0, self.viewOne.frame.size.width, 25);
    [self.viewOne addSubview:self.titleLab];
    
    self.viewTwo=[[UIView alloc] init];
    self.viewTwo.backgroundColor=[UIColor whiteColor];
    self.viewTwo.frame=CGRectMake(15, CGRectGetMaxY(self.viewOne.frame)+15, self.view.frame.size.width-30, HH+kNavbarHeight + kBottomSafeHeight);
    [self.backgroundViews addSubview:self.viewTwo];
    
    self.descLabel=[[UILabel alloc] init];
    [self.descLabel  setAttributedText:setString];
    self.descLabel.numberOfLines=0;
    self.descLabel.alpha=0.55;
    self.descLabel.frame=CGRectMake(0, 0, self.viewTwo.frame.size.width,HH);
    self.descLabel.font=[UIFont boldSystemFontOfSize:14];
    [self.descLabel sizeToFit];
    [self.viewTwo addSubview:self.descLabel];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
